package com.rcl.himalaya_kotlin.presenter

import com.rcl.himalaya_kotlin.`interface`.IRecommendPresenter
import com.rcl.himalaya_kotlin.`interface`.IRecommendViewCallback
import com.rcl.himalaya_kotlin.fragment.RecommendFragment
import com.rcl.himalaya_kotlin.utils.Constants
import com.rcl.himalaya_kotlin.utils.LogUtil
import com.ximalaya.ting.android.opensdk.constants.DTransferConstants
import com.ximalaya.ting.android.opensdk.datatrasfer.CommonRequest
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack
import com.ximalaya.ting.android.opensdk.model.album.Album
import com.ximalaya.ting.android.opensdk.model.album.GussLikeAlbumList
import java.util.HashMap

/*
 * class title: 
 * Author by RenChunLin, Email 18957806320@163.com, Date on 2020/9/11.
 * PS: Not easy to write code, please indicate.
 */
object RecommendPresenter : IRecommendPresenter {

    private val TAG = "RecommendPresenter"
    private val mCallback = ArrayList<IRecommendViewCallback>()

    override fun getRecommendList() {
        //封装参数
        val map: MutableMap<String, String> = HashMap()
        //这个参数表示一页数据返回多少条
        map[DTransferConstants.LIKE_COUNT] = Constants.RECOMMEND_COUNT.toString()
        CommonRequest.getGuessLikeAlbum(map, object : IDataCallBack<GussLikeAlbumList?> {
            override fun onSuccess(gussLikeAlbumList: GussLikeAlbumList?) {
                //数据获取成功
                if (gussLikeAlbumList != null) {
                    val albumList = gussLikeAlbumList.albumList
                    handlerRecommendResult(albumList)
                }
            }

            override fun onError(i: Int, s: String) {
                //数据获取失败
                LogUtil.e(TAG, "error---->$i")
                LogUtil.e(TAG, "errorMsg---->$s")
            }
        })
    }

    private fun handlerRecommendResult(albumList: List<Album>) {
        for (callback in mCallback) {
            callback.onRecommendListLoaded(albumList)
        }
    }

    override fun registerViewCallback(callback: IRecommendViewCallback) {
        if (mCallback.contains(callback)) {
            mCallback.add(callback)
        }
    }

    override fun unRegisterViewCallback(callback: IRecommendViewCallback) {
        mCallback.remove(callback)
    }

}